package com.yanqu.road.dao.impl.activity.hanghai;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hanghai.HangHaiLockStatus;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class HangHaiLockStatusDaoImpl extends TempDataObjectDao<HangHaiLockStatus> {

    public static String getHangHaiLockStatusSql = "select * from `t_u_activity_hanghai_lock_status` where `activityId`=?";

    public HangHaiLockStatus getHangHaiLockStatus(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getHangHaiLockStatusSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateHangHaiLockStatusSql = "update `t_u_activity_hanghai_lock_status` set hadLock=? where activityId=?";

    @Override
    protected boolean updateData(HangHaiLockStatus hangHaiLockStatus) {
        hangHaiLockStatus.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, hangHaiLockStatus.isHadLock()));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiLockStatus.getActivityId()));
        if (getDb().executeNoneQuery(updateHangHaiLockStatusSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHangHaiLockStatusSql = "insert into `t_u_activity_hanghai_lock_status`(activityId,hadLock) values (?,?)";

    @Override
    protected boolean addData(HangHaiLockStatus hangHaiLockStatus) {
        hangHaiLockStatus.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiLockStatus.getActivityId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, hangHaiLockStatus.isHadLock()));
        if (getDb().executeNoneQuery(insertHangHaiLockStatusSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HangHaiLockStatus parseData(ResultSet rs) throws SQLException {
        HangHaiLockStatus hangHaiLockStatus = new HangHaiLockStatus();
        hangHaiLockStatus.setActivityId(rs.getInt("activityId"));
        hangHaiLockStatus.setHadLock(rs.getBoolean("hadLock"));
        return hangHaiLockStatus;
    }

}
